package com.quan.server;

import com.mark.util.wxmp.WeixinMPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.quan.server.client.RestClient;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class Config {
    private static Logger logger = LoggerFactory.getLogger(Config.class);
    @Value("${qunWeixinMP.appId}")
    public String qunWeixinMPAppId;
    @Value("${qunWeixinMP.appSecret}")
    public String qunWeixinMPAppSecret;
    @Value("${qunWeixinMP.token}")
    public String qunWeixinMPToken;

    @Value("${qunWeixinMP.domain-name}")
    public String domainName;

    @Value("${isDebug}")
    public boolean isDebug = false;

    private static Map properties = new HashMap();

    public static Map getProperties() {
        return properties;
    }

    public static String getProperty(String key) {
        Object value = properties.get(key);
        return value == null ? "" : value.toString();
    }

    public static void setProperty(String key, Object value) {
        properties.put(key, value);
    }

    @Value("${spring.profiles.active}")
    public String profileActive;

    public boolean isGamma() {
        return "gamma".equals(profileActive);
    }

    public boolean isAlpha() {
        return "alpha".equals(profileActive);
    }

    @PostConstruct
    public void init() {
        logger.error("isGamma:" + isGamma());
        if (isGamma()) {
            new Thread(() -> {
                while (true) {
                    String accessToken = getNewAccessToken();
                    setProperty(Const.CONFIG_KEY_ACCESS_TOKEN, accessToken);
                    //获取成功，停7000s
                    try {
                        TimeUnit.SECONDS.sleep(600);
                    } catch (Exception e) {
                        logger.error("", e);
                    }
                }
            }).start();
        } else {
            new Thread(() -> {
                while (true) {
                    Map configProperties = getConfigProperties();
                    getProperties().putAll(configProperties);
                    try {
                        TimeUnit.SECONDS.sleep(300);
                    } catch (Exception e) {
                        logger.error("", e);
                    }
                }
            }).start();
        }
    }

    public String getNewAccessToken() {
        while (true) {
            String appId = qunWeixinMPAppId;
            String appSecret = qunWeixinMPAppSecret;
            logger.error("appId:" + appId);
            logger.error("appSecret:" + appSecret);
            String accessToken = WeixinMPClient.getAccessToken(appId, appSecret);
            logger.error("accessToken:" + appSecret);
            if (accessToken != null) {
                return accessToken;
            }
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (Exception e) {
                logger.error("", e);
            }
        }
    }

    public Map getConfigProperties() {
        while (true) {
            Map configMap = RestClient.getConfigFromGamma();
            if (configMap != null) {
                return configMap;
            }
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {
                logger.error("", e);
            }
        }
    }

    public String getAccessToken() {
        return getProperties().get(Const.CONFIG_KEY_ACCESS_TOKEN).toString();
    }
}
